// pages/guide/guide.js
const api = require("../../comm/api")
const http = require("../../utils/http")
const util = require("../../utils/util")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        guides: [],
        autoplay: true,
        interval: 500,
        current: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        http.getJSON(api.AdRotation.Guide.PATH, pojo => {
            this.setData({
                guides: pojo.data
            })
            console.log(this.data.guides)
        })
        const user = util.readUser()
        if (!!user) {
            // 已经登录过，尝试自动登录
            http.postJSON(api.User.Login.PATH, user, pojo => {
                util.saveToken(pojo.token)
                this.onTrail()
            })
        }
    },

    onChange(e) {
        const current = e.detail.current
        const endPageNo = this.data.guides.length - 1
        this.setData({
            autoplay: current != endPageNo
        })
    },

    onTrail() {
        wx.switchTab({
            url: '/pages/home/home',
        })
    }
})